package com.ofa.dao;

import java.util.List;

import org.springframework.stereotype.Component;

import com.ofa.base.HibernateDAOImpl;
import com.ofa.base.Pagination;
import com.ofa.entity.Person;
import com.ofa.entity.User;


@Component(value="userDao")
public class UserDao extends HibernateDAOImpl<User> {

	public void save(User user, Integer personId) {
		if(personId != null)
			user.setPerson((Person)this.getHibernateTemplate().get(Person.class, personId));
		getHibernateTemplate().save(user);
	}

	public void update(User user, Integer personId) {
		if(personId != null)
			user.setPerson((Person)this.getHibernateTemplate().get(Person.class, personId));
		getHibernateTemplate().update(user);
	}
	

	public Pagination<User> queryUsers(User user, int firstResult, int maxResults){
		String hql = "select u from User u where 1=1 ";
		
		List<User> list = this.getSession()
				.createQuery(hql)
				.setFirstResult(firstResult)
				.setMaxResults(maxResults)
				.list();

		String countHql = "select count(u.id) from User u where 1=1 ";
		int total = ((Long)(this.getSession().createQuery(countHql).uniqueResult())).intValue();
		
		Pagination<User> pagination = new Pagination<User>();
		pagination.setData(list);
		pagination.setTotal(total);
		return pagination;
	}
	
	
	
}
